package com.eastfair.venueservice.controller;

import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.venueservice.dto.ServiceOrderDTO;
import com.eastfair.venueservice.entity.ServiceOrder;
import com.eastfair.venueservice.service.HomeRepairServiceOrderService;
import com.eastfair.venueservice.vo.ServiceOrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 主场报修订单 REST API.
 *
 * @author dqq
 * @date 2023 /4/26
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/homeRepairServiceOrder")
@Api(value = "HomeRepairServiceOrder", tags = "主场报修订单")
@PreAuth(replace = "common:serviceOrder:")
public class HomeRepairServiceOrderController {

    @Resource
    private HomeRepairServiceOrderService homeRepairServiceOrderService;

    /**
     * 订单编号获取服务订单VO.
     *
     * @param orderNumber the order number
     * @return the by id
     */
    @ApiOperation("订单编号获取服务订单VO")
    @GetMapping("/get/{orderNumber}")
    public R<ServiceOrderVO> getById(@PathVariable("orderNumber") String orderNumber) {
        log.info("getById - 订单编号获取服务订单VO, orderNumber={}", orderNumber);
        try {
            return R.successDef(homeRepairServiceOrderService.getVOByOrderNumber(orderNumber));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 保存订单.
     *
     * @param serviceOrderDTO the service order dto
     * @return the r
     */
    @ApiOperation("保存服务订单")
    @SysLog("添加服务订单")
    @PostMapping("/save")
    public R<ServiceOrder> saveServiceOrder(@RequestBody ServiceOrderDTO serviceOrderDTO) {
        log.info("saveServiceOrder - 保存服务订单, serviceOrderDTO={}", serviceOrderDTO);
        try {
            return R.successDef(homeRepairServiceOrderService.saveServiceOrderForBuilder(serviceOrderDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 修改服务订单.
     *
     * @param serviceOrderDTO the service order dto
     * @return the r
     */
    @ApiOperation("修改服务订单")
    @SysLog("修改服务订单")
    @PostMapping("/update")
    public R<ServiceOrder> updateServiceOrder(@RequestBody ServiceOrderDTO serviceOrderDTO) {
        log.info("updateServiceOrder - 修改服务订单, serviceOrderDTO={}", serviceOrderDTO);
        try {
            return R.successDef(homeRepairServiceOrderService.updateServiceOrder(serviceOrderDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 提交搭建商的服务订单和管理费订单.
     *
     * @param orderNumberList the order number
     * @return the r
     */
    @ApiOperation("提交订单")
    @SysLog("提交服务订单")
    @PostMapping("/submit")
    public R<Boolean> submit(@RequestBody List<String> orderNumberList) {
        log.info("submit - 提交订单, orderNumber={}", orderNumberList);
        try {
            return R.successDef(homeRepairServiceOrderService.submitServiceOrder(orderNumberList));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 取消订单.
     *
     * @param orderNumber the order number
     * @return the r
     */
    @ApiOperation("取消订单")
    @SysLog("取消服务订单")
    @GetMapping("/cancel/{orderNumber}")
    public R<Boolean> cancel(@PathVariable("orderNumber") String orderNumber) {
        log.info("cancel - 取消订单, orderNumber={}", orderNumber);
        try {
            return R.successDef(homeRepairServiceOrderService.cancelServiceOrder(orderNumber));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 支付订单.
     *
     * @param serviceOrderDTO the service order dto
     * @return the r
     */
    @SysLog("支付服务订单")
    @PostMapping("/pay")
    public R<Boolean> payServiceOrder(@RequestBody ServiceOrderDTO serviceOrderDTO) {
        log.info("payServiceOrder - 支付订单, serviceOrderDTO={}", serviceOrderDTO);
        return R.successDef(homeRepairServiceOrderService.payServiceOrder(serviceOrderDTO));
    }
}